package com.aimee.servlet;

import com.aimee.dao.QuestionsDao;
import com.google.gson.Gson;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/admin/question")
public class QuestionServlet extends BaseServlet {

    /**
     * 展示题目关联用户表和题库表
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public void show(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        Gson gson = new Gson();

        QuestionsDao questionsDao = new QuestionsDao();
        try {
            List list = questionsDao.show();
            String json = gson.toJson(list);
            System.out.println(json);

            String res = "{\n" +
                    "  \"code\": 0,\n" +
                    "  \"msg\": \"\",\n" +
                    "  \"count\": 10,\n" +
                    "  \"data\": " + json + "\n" +
                    "} ";
            System.out.println(res);
            /*int count = 0;
            for (int i = 0; i < res.length()-2; i++) {
                if (count==1){
                    if (res.charAt(i)=='[') {
                    }
                }
            }*/
            PrintWriter out = response.getWriter();
            out.write(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param request
     * @param response
     * @throws IOException
     */
    public void del(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        String id = request.getParameter("id");
        int question_id = Integer.parseInt(id);
        QuestionsDao questionsDao = new QuestionsDao();
        try {
            int i = questionsDao.del(question_id);
            out.write(i);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加一个
     * @param request
     * @param response
     * @throws IOException
     */
    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        String qeus[] = new String[10];

        String couse_name = request.getParameter("course");
        String question_type = request.getParameter("type");
        String id = request.getParameter("user_id");
        int user_id = Integer.parseInt(id);
        String question_content = request.getParameter("content");
        String optionA = request.getParameter("optionA");
        String optionB = request.getParameter("optionB");
        String optionC = request.getParameter("optionC");
        String optionD = request.getParameter("optionD");
        String answer = request.getParameter("answer");
        Date date = new Date(System.currentTimeMillis());

        qeus[0] = couse_name;
        qeus[1] = id;
        qeus[2] = question_content;
        qeus[3] = question_type;
        System.out.println(question_type);
        qeus[4] = optionA;
        qeus[5] = optionB;
        qeus[6] = optionC;
        qeus[7] = optionD;
        qeus[8] = answer;
        qeus[9] = date.toString();

        QuestionsDao questionsDao = new QuestionsDao();
        int i = 0;
        try {
            i = questionsDao.add(qeus);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        out.write(i);

    }


}
